/**
 * Copyright (C) 2009 Matteo Merli <mm@merlimat.org>
 * 
 * $Id: PxlMap.java 24 2009-01-19 19:49:05Z matteo.merli $
 * $URL: http://pxl-lang.googlecode.com/svn/trunk/src/main/java/pxl/types/PxlMap.java $
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *         http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package pxl.types;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import pxl.errors.KeyError;
import pxl.types.annotations.Exported;

import com.google.common.collect.Maps;

public class PxlMap extends PxlObject
{

    private final Map<PxlObject, PxlObject> map;

    public PxlMap()
    {
        map = Maps.newHashMap();
    }

    public PxlMap( int size )
    {
        map = Maps.newHashMapWithExpectedSize( size );
    }

    public PxlMap( Map<PxlObject, PxlObject> map )
    {
        this.map = Maps.newHashMap( map );
    }

    @Override
    public String getType()
    {
        return "dict";
    }

    @Override
    public String toString()
    {
        Iterator<Entry<PxlObject, PxlObject>> it = map.entrySet().iterator();
        if ( !it.hasNext() )
            return "{}";

        Entry<PxlObject, PxlObject> entry;
        PxlObject key, value;
        StringBuilder sb = new StringBuilder();
        sb.append( '{' );

        while ( true )
        {
            entry = it.next();
            key = entry.getKey();
            value = entry.getValue();

            sb.append( key == this ? "(this Map)" : key.__repr__() );
            sb.append( ": " );
            sb.append( value == this ? "(this Map)" : value.__repr__() );

            if ( !it.hasNext() )
                return sb.append( '}' ).toString();

            sb.append( ", " );
        }
    }

    public Map<PxlObject, PxlObject> getValue()
    {
        return map;
    }

    // // Operators

    @Exported
    @Override
    public PxlString __repr__()
    {
        return new PxlString( toString() );
    }

    @Exported
    @Override
    public PxlString __str__()
    {
        return __repr__();
    }

    @Exported
    @Override
    public PxlBoolean __bool__()
    {
        return PxlBoolean.valueOf( map.isEmpty() );
    }

    @Exported
    @Override
    public PxlObject __getitem__( PxlObject key )
    {
        PxlObject value = map.get( key );

        if ( value == null )
            throw new KeyError( key );

        return value;
    }

    @Exported
    @Override
    public void __setitem__( PxlObject key, PxlObject value )
    {
        map.put( key, value );
    }

    @Exported
    @Override
    public PxlIterator __getiterator__()
    {
        return new PxlBaseIterator( map.keySet().iterator() );
    }

    @Exported
    public void put( PxlObject key, PxlObject value )
    {
        map.put( key, value );
    }

    @Exported
    public PxlObject keys()
    {
        return new PxlList( map.keySet() );
    }

    @Exported
    public PxlObject values()
    {
        return new PxlList( map.values() );
    }

    // Attributes

    private static final Map<String, PxlObject> attrs = PxlObject
            .getExportedMethods( PxlMap.class );

    @Override
    protected PxlObject getAttribute( String name )
    {
        return attrs.get( name );
    }

    @Override
    protected Collection<String> getAttributeNames()
    {
        return attrs.keySet();
    }

}
